package com.example.planetexploder.utils.screenshot;

import android.annotation.SuppressLint;
import android.widget.ImageView;

import com.example.planetexploder.bean.otherdata.CutRatio;

/**
 * 矩形拖动框
 * @author 宋熙然
 * 修改日期：2019.10.24
 */
public class MyRect {
    private ImageView iv;
    private ImageView p1;
    private ImageView p2;
    private ImageView p3;
    private ImageView p4;

    private MyOnTouchListener listener1;
    private MyOnTouchListener listener2;
    private MyOnTouchListener listener3;
    private MyOnTouchListener listener4;

    private int pRadius;
    private int max_up;
    private int max_down;
    private int max_left;
    private int max_right;

    private int ivWidth;
    private int ivHeight;

    public MyRect(ImageView iv, ImageView p1, ImageView p2, ImageView p3, ImageView p4) {
        this.iv = iv;
        this.p1 = p1;
        this.p2 = p2;
        this.p3 = p3;
        this.p4 = p4;
    }

    public CutRatio getCutRatio() {
        CutRatio cr = new CutRatio();
        int x1 = p1.getLeft() + pRadius;
        int y1 = p1.getTop() + pRadius;
        int x2 = p2.getLeft() + pRadius;
        int y4 = p4.getTop() + pRadius;

        if (x1 < x2) {
            cr.rx1 = (double) (x1 - iv.getLeft()) / ivWidth;
            cr.rx2 = (double) (x2 - iv.getLeft()) / ivWidth;
        } else {
            cr.rx1 = (double) (x2 - iv.getLeft()) / ivWidth;
            cr.rx2 = (double) (x1 - iv.getLeft()) / ivWidth;
        }
        if (y1 < y4) {
            cr.ry1 = (double) (y1 - iv.getTop()) / ivHeight;
            cr.ry2 = (double) (y4 - iv.getTop()) / ivHeight;
        } else {
            cr.ry1 = (double) (y4 - iv.getTop()) / ivHeight;
            cr.ry2 = (double) (y1 - iv.getTop()) / ivHeight;
        }

        return cr;
    }

    @SuppressLint("ClickableViewAccessibility")
    public void setMeasure() {
        ivWidth = iv.getWidth();
        ivHeight = iv.getHeight();
        pRadius = p1.getWidth() / 2;
        max_up = iv.getTop() - pRadius;
        max_down = iv.getTop() + ivHeight - pRadius;
        max_left = iv.getLeft() - pRadius;
        max_right = iv.getLeft() + ivWidth - pRadius;

        MoveWidget.moveImageView(p1, max_left, max_up);
        MoveWidget.moveImageView(p2, max_right, max_up);
        MoveWidget.moveImageView(p3, max_right, max_down);
        MoveWidget.moveImageView(p4, max_left, max_down);

        listener1 = new MyOnTouchListener(p1, p2, p4, max_up, max_down, max_left, max_right);
        listener2 = new MyOnTouchListener(p2, p1, p3, max_up, max_down, max_left, max_right);
        listener3 = new MyOnTouchListener(p3, p4, p2, max_up, max_down, max_left, max_right);
        listener4 = new MyOnTouchListener(p4, p3, p1, max_up, max_down, max_left, max_right);

        p1.setOnTouchListener(listener1);
        p2.setOnTouchListener(listener2);
        p3.setOnTouchListener(listener3);
        p4.setOnTouchListener(listener4);
    }


}
